﻿using ZingFrameworkCore.Common;
using ZingFrameworkCore.Common.Cache.Cookie;
using _HttpContext = ZingFrameworkCore.Common.Cache.HttpContext;
namespace ZingBlog.Web
{
    public class OperatorProvider
    {
        public static OperatorProvider Provider
        {
            get { return new OperatorProvider(); }
        }
        private string LoginUserKey = "loginkey_2018";
        private string LoginProvider = Configs.GetValue("LoginProvider");

        public OperatorModel GetCurrent()
        {
            OperatorModel operatorModel = new OperatorModel();
            if (LoginProvider == "Cookie")
            {
                operatorModel = DES.Decrypt(HttpContextCookie.Get(LoginUserKey).ToString()).ToObject<OperatorModel>();
            }
            else
            {
                operatorModel = DES.Decrypt(_HttpContext.Current.Session.GetString(LoginUserKey)).ToObject<OperatorModel>();
            }
            return operatorModel;
        }
        public void AddCurrent(OperatorModel operatorModel)
        {
            if (LoginProvider == "Cookie")
            {
                HttpContextCookie.Write(LoginUserKey, DES.Encrypt(operatorModel.ToJson()), 60);
            }
            else
            {
                _HttpContext.Current.Session.SetString(LoginUserKey, DES.Encrypt(operatorModel.ToJson()));
            }
        }
        public void RemoveCurrent()
        {
            if (LoginProvider == "Cookie")
            {
                HttpContextCookie.Remove(LoginUserKey.Trim());
            }
            else
            {
                _HttpContext.Current.Session.Remove(LoginUserKey.Trim());
            }
        }
    }
}
